#include "heroplane.h"
#include "config.h"

HeroPlane::HeroPlane()
{
    //加载飞机图片
    m_Plane.load(HERO_PATH);
    m_Plane2.load(HERO_PATH2);
    m_PlaneDown[0].load(HERO_DOWN1);
    m_PlaneDown[1].load(HERO_DOWN2);
    m_PlaneDown[2].load(HERO_DOWN3);
    m_PlaneDown[3].load(HERO_DOWN4);
    m_PlaneDown[4].load(HERO_DOWN5);
    m_PlaneDown[5].load(HERO_DOWN6);
    m_PlaneDown[6].load(HERO_DOWN7);
    //初始化图片指针
    p_Plane = &m_Plane;

    //生命图标
    m_Icon.load(GAME_ICON);


    //初始化坐标
    m_X = (GAME_WIDTH - m_Plane.width())/ 2;
    m_Y = GAME_HEIGHT - m_Plane.height();

    //初始化矩形边框
    m_Rect.setWidth(m_Plane.width()-HERO_RECT*2);
    m_Rect.setHeight(m_Plane.height()-HERO_RECT*2);
    m_Rect.moveTo(m_X+HERO_RECT, m_Y+HERO_RECT);

    //初始化子弹发射间隔计数
    m_Interval = 0;
    //子弹发射位置
    m_ShootPlace = false;
    m_ShootX = 0;
    m_ShootY = 0;

    //初始化图片切换计数
    m_PicCycle = 0;

    //生命
    m_life = HERO_LIFE;

    //爆炸状态
    m_Down = -1;

}

/* 发射子弹 */
void HeroPlane::shoot()
{
    //爆炸时不射击
    if(m_Down >= 0 && m_Down < HERO_DOWN_TIME)
    {
        m_Interval = 0;
        return;
    }

    m_Interval++;
    //当间隔达到发射
    if(m_Interval >= BULLET_INTERVAL)
    {
        m_Interval = 0; //重置间隔
        //寻找空闲子弹
        for(int i=0; i < BULLET_NUM; i++)
        {
            if(m_Bullets[i].m_Free)
            {
                m_Bullets[i].m_Free = false;
                m_Bullets[i].m_X = m_X + m_Rect.width()*0.5 - 10;
                m_Bullets[i].m_Y = m_Y - 25 ;
                break;
            }
        }
    }
}

/* 设置飞机位置 */
void HeroPlane::setPosition(int x, int y)
{

    m_X = x - m_Plane.width()/ 2;
    m_Y = y - m_Plane.height()/ 2;
    //重生时图片调整位置
    if(m_Down >= HERO_DOWN_TIME)
    {
        m_X += (m_Plane.width()-m_Icon.width())/ 2;
        m_Y += (m_Plane.height()-m_Icon.height())/ 2;
    }

    //重生时保持飞机在屏幕内
    if(m_Down >= HERO_DOWN_TIME)
    {
        if(m_X < 0)
        {
            m_X = 0;
        }
        if(m_Y < 0)
        {
            m_Y = 0;
        }
        if(m_X > GAME_WIDTH-m_Icon.width())
        {
            m_X = GAME_WIDTH-m_Icon.width();
        }
        if(m_Y > GAME_HEIGHT-m_Icon.height())
        {
            m_Y = GAME_HEIGHT-m_Icon.height();
        }
    }
    else
    {
        //保持飞机在屏幕内
        if(m_X < 0)
        {
            m_X = 0;
        }
        if(m_Y < 0)
        {
            m_Y = 0;
        }
        if(m_X > GAME_WIDTH-m_Plane.width())
        {
            m_X = GAME_WIDTH-m_Plane.width();
        }
        if(m_Y > GAME_HEIGHT-m_Plane.height())
        {
            m_Y = GAME_HEIGHT-m_Plane.height();
        }
    }

    //矩形边框移动
    m_Rect.moveTo(m_X+HERO_RECT, m_Y+HERO_RECT);
}

/* 飞机图像切换 */
void HeroPlane::picCycle()
{

    //爆炸重生处理
    if(m_Down >= 0)
    {
        //爆炸动画
        if(m_Down < HERO_DOWN_TIME-HERO_DOWN_INTERVAL && m_Down%HERO_DOWN_INTERVAL == 0)
        {
            p_Plane = &m_PlaneDown[(m_Down/HERO_DOWN_INTERVAL)%3];
        }
        //结束动画
        else if(m_Down == HERO_DOWN_TIME-HERO_DOWN_INTERVAL )
        {
            p_Plane = &m_PlaneDown[3];
        }
        //重生
        else if(m_Down == HERO_DOWN_TIME)
        {
            //重生时图片调整位置
            m_X += (m_Plane.width()-m_Icon.width())/ 2;
            m_Y += (m_Plane.height()-m_Icon.height())/ 2;

            p_Plane = &m_Icon;
            m_life--;
        }
        //结束
        else if(m_Down >= HERO_TIME)
        {
            //结束时图片调整位置
            m_X -= (m_Plane.width()-m_Icon.width())/ 2;
            m_Y -= (m_Plane.height()-m_Icon.height())/ 2;
            m_Rect.moveTo(m_X + HERO_RECT, m_Y + HERO_RECT);

            p_Plane = &m_Plane;
            m_Down = -1;
            return;
        }
        m_Down++;
        return;
    }

    //图像切换
    m_PicCycle++;
    if(m_PicCycle == HERO_PIC_CYCLE/2)
    {
        p_Plane = &m_Plane2;
    }
    if(m_PicCycle >= HERO_PIC_CYCLE)
    {
        p_Plane = &m_Plane;
        m_PicCycle = 0;
    }
}













